package com.gy.api.pc.enterprise.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.gy.api.base.Constants;
import com.gy.api.base.Type;
import com.gy.api.mobile.company.dao.CompanyInfoDao;
import com.gy.api.mobile.company.domain.po.CompanyInfoPO;
import com.gy.api.mobile.company.service.ICompanyService;
import com.gy.api.mobile.msg.dao.UserMessagesDao;
import com.gy.api.pc.enterprise.domain.vo.*;
import com.gy.api.pc.enterprise.service.IEnterpriseService;
import com.gy.api.pc.examine.dao.ExamineDao;
import com.gy.api.pc.examine.dao.ExamineLogDao;
import com.gy.api.pc.examine.domain.po.ExamineLogPO;
import com.gy.api.pc.examine.domain.po.ExaminePO;
import com.gy.api.pc.examine.domain.vo.ExamineLogAddVO;
import com.gy.api.pc.examine.domain.vo.ExamineLogVO;
import com.gy.api.pc.examine.domain.vo.SubmitLogVO;
import com.gy.api.pc.examine.service.IExamineService;
import com.gy.api.pc.sys.user.dao.SysUserDao;
import com.gy.api.pc.sys.user.domain.po.SysUserPO;
import com.gy.exception.BusinessException;
import com.gy.task.UserMessageThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;


@Service
@Transactional
@Slf4j
public class EnterpriseServiceImpl implements IEnterpriseService {

    @Autowired
    ExamineDao examineDao;
    @Autowired
    ExamineLogDao examineLogDao;
    @Autowired
    private ICompanyService companyService;
    @Autowired
    private IExamineService examineService;
    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private CompanyInfoDao companyInfoDao;


    @Override
    public List<EnterpriseVO> getList(EnterpriseSearchVO searchVO) {
        return examineDao.getCompanyList(searchVO);
    }

    @Override
    public List<EnterpriseVO> getListOne(EnterpriseSearchVO searchVO) {
//        searchVO.setNodeName(Constants.EXAMINE_ENTERPRISE_CERTIFICATION[1]);
        return examineDao.getCompanyList(searchVO);
    }

    @Override
    public EnterpriseDescVO getDesc(String id) {
        ExaminePO examinePO=examineDao.selectByPrimaryKey(id);

        if(examinePO==null){
            throw new BusinessException(Type.FAIL.getCode(),"审核信息未找到！");
        }
        EnterpriseDescVO enterpriseDescVO=new EnterpriseDescVO();
        enterpriseDescVO.setListOne(examineDao.getCompanyOne(examinePO.getId()));

        BeanUtils.copyProperties(examinePO, enterpriseDescVO);
        //查询管理企业信息
        enterpriseDescVO.setCompanyInfoVO(companyService.getCompanyDesc(examinePO.getRelatedId()));
        //查询审核记录
        Example example=new Example(ExamineLogPO.class);
        example.and().andEqualTo("examineId",examinePO.getId());
        example.setOrderByClause("examine_date desc");
        List<ExamineLogPO> examineLogPOS=examineLogDao.selectByExample(example);
        List<ExamineLogVO> examineLogVOList=new ArrayList<>();
        for (ExamineLogPO examineLogPO:examineLogPOS){
            ExamineLogVO examineLogVO=new ExamineLogVO();
            BeanUtils.copyProperties(examineLogPO, examineLogVO);
            examineLogVOList.add(examineLogVO);
        }
        enterpriseDescVO.setExamineLogList(examineLogVOList);
        //查询提交记录
        Example example1=new Example(ExaminePO.class);
        example1.and().andEqualTo("relatedId",enterpriseDescVO.getCompanyInfoVO().getId());
        example1.setOrderByClause("create_date desc");
        List<ExaminePO> examinePOList=examineDao.selectByExample(example1);
        List<SubmitLogVO> submitLogVOS=new ArrayList<>();
        Integer order=0;
        for (ExaminePO examinePO1:examinePOList){
            SubmitLogVO submitLogVO=new SubmitLogVO();
            submitLogVO.setExamineId(examinePO1.getId());
            submitLogVO.setExamineState(examinePO1.getExamineState());
            submitLogVO.setCreateDate(examinePO1.getCreateDate());
            submitLogVO.setSubmitOrder("第"+(examinePOList.size()-order)+"次提交");
            submitLogVOS.add(submitLogVO);
            order++;
        }
        enterpriseDescVO.setSubmitLogList(submitLogVOS);
        return enterpriseDescVO;
    }


    @Autowired
    private UserMessagesDao userMessagesDao;
    @Override
    public void completeTask(EnterpriseCompleteVO taskCompleteDto) {
        ExaminePO examinePO=examineDao.selectByPrimaryKey(taskCompleteDto.getId());
        if(examinePO==null){
            throw new BusinessException(Type.FAIL.getCode(),"审核信息未找到！");
        }
        if(examinePO.getExamineState()!=1){
            throw new BusinessException(Type.FAIL.getCode(),"流程不是待审核状态，请勿重复操作！");
        }
        SysUserPO sysUserPO=sysUserDao.selectByPrimaryKey(StpUtil.getLoginIdAsString());
        if(sysUserPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"用户信息未找到！");
        }
        if(examinePO.getExamineUserIds().indexOf(sysUserPO.getId())<0){
            throw new BusinessException(Type.FAIL.getCode(),"当前用户不是该节点的审批人！");
        }
        CompanyInfoPO companyInfoPO=companyInfoDao.selectByPrimaryKey(examinePO.getRelatedId());
        String EXAMINE_LOG_AGREE=Constants.EXAMINE_LOG_AGREE;
        String EXAMINE_ONE_NODE=examinePO.getExamineNode();
        if(examinePO.getExamineNode().equals(Constants.EXAMINE_ENTERPRISE_CERTIFICATION[2])&&taskCompleteDto.getOutcome()==1){
            //审查决定进入下一阶段
            examinePO.setExamineState(2);
            examinePO.setExamineUserIds("");
            examinePO.setExamineNode(Constants.EXAMINE_ENTERPRISE_CERTIFICATION[3]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);
            companyInfoPO.setCompanyState(1);
            companyInfoPO.preUpdate();
            companyInfoDao.updateByPrimaryKeySelective(companyInfoPO);

            new UserMessageThread(userMessagesDao,"1",
                    "贵单位【"+companyInfoPO.getCompanyName()+"】的企业认证已通过。现您可办理相关业务事项。",
                    companyInfoPO.getId()).start();

        }
        if(examinePO.getExamineNode().equals(Constants.EXAMINE_ENTERPRISE_CERTIFICATION[1])&&taskCompleteDto.getOutcome()==1){
            //初审进入下一阶段
            examinePO.setExamineState(1);
            examinePO.setExamineUserIds(Constants.EXAMINE_1_USER);
            examinePO.setExamineNode(Constants.EXAMINE_ENTERPRISE_CERTIFICATION[2]);
            examinePO.preUpdate();
            examineDao.updateByPrimaryKeySelective(examinePO);
        }
        if(taskCompleteDto.getOutcome()!=1){
            log.info("审核失败》》》取消流程::::::::"+taskCompleteDto.getOutcome());
            //审核失败  取消流程
            examinePO.setExamineState(taskCompleteDto.getOutcome()+1);
            examinePO.preUpdate();
            EXAMINE_LOG_AGREE=taskCompleteDto.getOutcome()==2?Constants.EXAMINE_LOG_DISAGREE:Constants.EXAMINE_LOG_RETURN;
            examineDao.updateByPrimaryKeySelective(examinePO);
            companyInfoPO.setCompanyState(2);
            companyInfoPO.preUpdate();
            companyInfoDao.updateByPrimaryKeySelective(companyInfoPO);

            if(examinePO.getExamineNode().equals(Constants.EXAMINE_ENTERPRISE_CERTIFICATION[1])){
                new UserMessageThread(userMessagesDao,"1",
                        "贵单位【"+companyInfoPO.getCompanyName()+"】的企业认证流程审核未能通过。您可在“个人中心-企业办事认证记录”中查看详情。",
                        companyInfoPO.getId()).start();
            }
            if(examinePO.getExamineNode().equals(Constants.EXAMINE_ENTERPRISE_CERTIFICATION[2])){
                new UserMessageThread(userMessagesDao,"1",
                        "贵单位【"+companyInfoPO.getCompanyName()+"】的企业认证审核未能通过。您可在“个人中心-企业办事认证记录”中查看详情。",
                        companyInfoPO.getId()).start();
            }
        }
        examineService.addExamineLog(new ExamineLogAddVO(examinePO.getRelatedId(), EXAMINE_LOG_AGREE,EXAMINE_ONE_NODE,sysUserPO.getUserName()
                ,sysUserPO.getId(),taskCompleteDto.getComment(),taskCompleteDto.getApprovalFileUrl(),taskCompleteDto.getApprovalFileName(),examinePO.getId()));
    }
}